package com.market.cash.ui.fragment

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.market.cash.R
import com.market.cash.databinding.CashUserBinding
import com.market.cash.pro.ProFragment
import com.market.cash.ui.activity.FeedbackActivity
import com.market.cash.ui.activity.KefuActivity
import com.market.cash.ui.activity.SettingActivity
import com.market.cash.ui.activity.UserInfoActivity
import com.market.cash.ui.presenter.CashRootUserPresenter
import com.market.cash.ui.view.ICashRootUserView
import com.market.cash.utils.CashRootImageUtil
import com.market.cash.utils.event.CashRootEventData
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper
import com.market.cash.utils.helper.CashRootCustomDataHelper
import com.market.cash.utils.helper.SharedHelper
import com.market.cash.weight.banner.entity.CashRootBannerInfo
import com.market.cash.weight.banner.transformers.Transformer

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/18 16:52
 * @Annotation：个人中心
 */
class UserFragment : ProFragment(), ICashRootUserView {
    lateinit var binding: CashUserBinding
    var presenter: CashRootUserPresenter? = null

    override fun getCashRootRootView(inflater: LayoutInflater): View {
        binding = CashUserBinding.inflate(inflater)
        return binding.root
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#F9F9F9"))
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootTextTitle(getString(R.string.me_title))
        setCashRootBackBtnVisibility(View.INVISIBLE)
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)
        initCashRootRefreshLayout()
        initCashRootBanner()
    }

    override fun initCashRootListener() {
        //客服
        rootViewBinding.imgKefu.setOnClickListener {
            KefuActivity.start(requireContext(), "User Center Page")
            addCashRootEventPoint(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER, "User Center Page")
        }

        //个人信息
        binding.userInfoLayout.setOnClickListener {
            onIntent(UserInfoActivity::class.java)
        }
        //昵称
        binding.textUserName.setOnClickListener {
            binding.userInfoLayout.callOnClick()
        }
        //评分
        binding.rateUsLayout.setOnClickListener {
            presenter!!.loadCashRootScoreCommentAction()
        }
        //联系客服
        binding.serviceLayout.setOnClickListener {
            rootViewBinding.imgKefu.callOnClick()
        }
        //意见反馈
        binding.feedbackLayout.setOnClickListener {
            FeedbackActivity.start(requireContext())
        }
        //设置
        binding.settingLayout.setOnClickListener {
            onIntent(SettingActivity::class.java)
        }
    }

    override fun initCashRootData() {
        presenter = CashRootUserPresenter(this)
        presenter!!.loadCashRootBannerDataAction()
        showCashRootUserInfo()

        //VIP 等级
        val grade = SharedHelper.getCashRootSPInt(SharedHelper.CashRoot_SPKEY.CASHROOT_VIP)
        when(grade){
            2 -> {
                binding.gradeLayout.setBackgroundResource(R.mipmap.cash_grade_gold_bg)
                binding.imgGradeIcon.setImageResource(R.mipmap.cash_grade_gold_icon)
                binding.textGrade.text = getString(R.string.home_grade_gold)
            }
            3 -> {
                binding.gradeLayout.setBackgroundResource(R.mipmap.cash_grade_platinum_bg)
                binding.imgGradeIcon.setImageResource(R.mipmap.cash_grade_platinum_icon)
                binding.textGrade.text = getString(R.string.home_grade_platinum)
            }
            else -> {
                binding.gradeLayout.setBackgroundResource(R.mipmap.cash_grade_silver_bg)
                binding.imgGradeIcon.setImageResource(R.mipmap.cash_grade_silver_icon)
                binding.textGrade.text = getString(R.string.home_grade_silver)
            }
        }

        //牌照信息
        if (!CashRootCustomConfigsHelper.isCashRootShowLicense()){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(initCashRootLicenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    /* 初始化刷新 */
    private fun initCashRootRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            //刷新产品列表
            publishEventData(CashRootEventData(CashRootEventData.TYPE.CASHROOT_PRODUCT_REFRESH))
            //刷新自定义数据
            CashRootCustomDataHelper.loadCashRootCustomDataAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /* 初始化banner */
    private fun initCashRootBanner() {
        // 设置XBanner的页面切换特效
        binding.xBanner.setPageTransformer(Transformer.Default)
        // 设置XBanner页面切换的时间，即动画时长
        binding.xBanner.setPageChangeDuration(1000)
        binding.xBanner.setAutoPlayTime(3000)
        binding.xBanner.setSlideScrollMode(1)
        //设置是否允许用户手指滑动
        binding.xBanner.setAllowUserScrollable(true)
        //是否自动轮播
        binding.xBanner.setAutoPlayAble(true)
        //设置非自动轮播状态下是否可以循环切换
        binding.xBanner.setHandLoop(true)
        //显示指示器
        binding.xBanner.setPointsIsVisible(true)
        binding.xBanner.setShowIndicatorOnlyOne(true)
        //是否开启一屏多个模式
        binding.xBanner.setIsClipChildrenMode(false)

        //加载广告图片
        binding.xBanner.loadImage { banner, model, view, position ->
            //2、返回的图片路径为Object类型，你只需要强转成你传输的类型就行，切记不要胡乱强转！
            val item = model as CashRootBannerInfo

            val itemLayout = view.findViewById<RelativeLayout>(R.id.itemLayout)
            itemLayout.setBackgroundResource(item.bannerResId)

            val textBanner = view.findViewById<TextView>(R.id.textBanner)
            textBanner.setTextColor(item.bannerTextColor)
            textBanner.text = item.bannerText
        }
    }

    /**
     * 设置用户头像和昵称
     */
    private fun showCashRootUserInfo(){
        val avatarUrl = CashRootCustomDataHelper.getCashRootCustomUserAvatarUrl()
        if (avatarUrl.isNotEmpty()){
            CashRootImageUtil.instance.loadImageUrl(requireContext(),binding.imgAvatar,avatarUrl)
        } else {
            binding.imgAvatar.setImageResource(R.mipmap.cash_holder)
        }
        binding.textUserName.text = CashRootCustomDataHelper.getCashRootCustomUserName()
    }

    /**
     * 设置banner
     */
    override fun showCashRootBannerList(bannerList: ArrayList<CashRootBannerInfo>) {
        binding.xBanner.setBannerData(R.layout.cash_banner_item,bannerList)
    }

    override fun onCashRootEvent(event: CashRootEventData) {
        when (event.eventType) {
            CashRootEventData.TYPE.CASHROOT_CUSTOM_DATA_REFRESHED -> {//自定义数据刷新完成
                //设置用户昵称和头像
                showCashRootUserInfo()
            }
            else -> {
                binding.refreshLayout.finishRefresh()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_USER_CENTER_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_USER_CENTER_END)
    }
}